<?php

/**
 * Subclass for representing a row from the 'contact' table.
 *
 * 
 *
 * @package lib.model
 */ 
class Contact extends BaseContact
{
  function Contact(){
    $this->setIden('');
    $this->setListName('');
    $this->setCompanyName('');
    $this->setCompanyInitials('');
    $this->setFirstName('');
    $this->setLastName('');
    $this->setMiddleName('');
    $this->setAddress('');
    $this->setCity('');
    $this->setPhone('');
    $this->setCellphone('');
    $this->setFax('');
    $this->setPobox('');
    $this->setTelex('');
    $this->setEmail('');
    $this->setPersonPosition('');
    $this->setCountryName('');
    $this->setSalutation('');
    $this->setNameSuffix('');
    $this->setWebsite('');
    $this->setZipCode('');
    $this->setRemarks('');
  }
  
  function setFieldsFromRequest(){
    $sf_params = sfContext::getInstance()->getRequest()->getParameterHolder();
    $this->setContactCategory($sf_params->get('contact_category',''));
    $this->setCompanyName($sf_params->get('company_name',''));
    $this->setCompanyInitials($sf_params->get('company_initials',''));
    if($sf_params->get('founding_day'))
      $this->setFoundingDay($sf_params->get('founding_day'));
    else
      $this->setFoundingDay(null);
    $this->setFirstName($sf_params->get('first_name',''));
    $this->setLastName($sf_params->get('last_name',''));
    $this->setMiddleName($sf_params->get('middle_name',''));
    if($sf_params->get('birthday'))
      $this->setBirthDay($sf_params->get('birthday'));
    else
      $this->setBirthDay(null);    
    $this->setAddress($sf_params->get('address',''));
    $this->setZipCode($sf_params->get('zip_code',''));
    $this->setCity($sf_params->get('city',''));
    $this->setPhone($sf_params->get('phone',''));
    $this->setCellphone($sf_params->get('cellphone',''));
    $this->setFax($sf_params->get('fax',''));
    $this->setEmail($sf_params->get('email',''));
    $this->setCountryName($sf_params->get('country_name',''));
    $this->setSalutation($sf_params->get('salutation',''));
    $this->setNameSuffix($sf_params->get('name_suffix',''));
    $this->setWebsite($sf_params->get('website',''));      
    $this->setPersonPosition($sf_params->get('person_position',''));      
    $this->setTelex($sf_params->get('telex',''));      
  }
  /*
  function getAccountSupplier(){
    $c = new Criteria();
    $c->add(AccountSupplierPeer::CONTACT_IDEN,$this->getIden());
    return AccountSupplierPeer::doSelectOne($c);
  }
  
  function getAccountCustomer(){
    $c = new Criteria();
    $c->add(AccountCustomerPeer::CONTACT_IDEN,$this->getIden());
    return AccountCustomerPeer::doSelectOne($c);
  }
  
  function getAccountOwner(){
    $c = new Criteria();
    $c->add(AccountOwnerPeer::CONTACT_IDEN,$this->getIden());
    return AccountOwnerPeer::doSelectOne($c);
  }
  */
  function getFullName(){
    return $this->getFirstName().' '.$this->getMiddleName('').' '.$this->getLastName();
  }
    
  function getFullAddress(){
    return nl2br($this->getAddress()).",<br/>".$this->getCity('')."<br/>".$this->getCountryName()."<br/>".$this->getZipCode();
  }
  
  function getCityAddress(){
    return $this->getAddress().",\n".$this->getCity('')."\n".$this->getZipCode();
  }
  
  function getFormalFullName(){
    return $this->getSalutation().' '.$this->getFirstName().' '.$this->getMiddleName('').' '.$this->getLastName().' '.$this->getNameSuffix('');
  }

  function save(PropelPDO $conn=null){
    if($this->getContactCategory()=='PERSON')
      $this->setListName(
        $this->getLastName().', '.
        $this->getFirstName().' '.
        $this->getMiddleName()
      );
    elseif($this->getContactCategory()=='COMPANY')
      $this->setListName(
        $this->getCompanyName()
      );
    elseif($this->getContactCategory()=='GOVERNMENT')
      $this->setListName(
        $this->getCompanyName()
      );
    parent::save($conn);        
  }

  function hasAccounts($except = null ){
    $account_owner = false;
    $account_customer = false;
    $account_supplier = false;
    $account_bank_count = 0;
    if(! isset($except['owner']))
      $account_owner = $this->getAccountOwner();
    if(! isset($except['customer']))
      $account_customer = $this->getAccountCustomer();
    if(! isset($except['supplier']))
      $account_supplier = $this->getAccountSupplier();
    if(! isset($except['bank']))
      $account_bank_count = $this->countAccountBanks();
        
    if(($account_owner || $account_customer || $account_supplier)||($account_bank_count > 0))
       return true;
     else
       return false;
    
  }

}


